<?php

App::uses('AdminAppController', 'Admin.Controller');
App::uses('AppController', 'Controller');

class TransactionsController extends AdminAppController {

    var $helpers = array('Form');

    function index() {
        $this->paginate = array(
            'fields' => array(
                'Transaction.*', 'User.*'
            ),
            'recursive' => -1,
            'joins' => array(
                array(
                    'table' => 'users',
                    'type' => 'left',
                    'alias' => 'User',
                    'conditions' => array('Transaction.user_id = User.id')
                )
            ),
            'limit' => 25,
            'order' => array('Transaction.id' => 'desc')
        );

        // we are using the 'Admin' model
        $rows = $this->paginate('Transaction');

        // pass the value to our view.ctp
        $this->set('rows', $rows);
    }

    function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $options['joins'] = array(
            array('table' => 'users',
                    'type' => 'left',
                    'alias' => 'User',
                    'conditions' => array('Transaction.user_id = User.id')
            )
        );
        $options['conditions'] = array('Transaction.id ' => $id);
        $options['fields'] = array('Transaction.*', 'User.*');
        $post = $this->Transaction->find('first', $options);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->request->data = $post;
    }

    function delete($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }

        if ($this->Transaction->delete($id)) {
            $this->Session->setFlash('Delete success!', 'default', array(), 'good');
        } else {
            $this->Session->setFlash('Delete unsuccess!', 'default', array(), 'bad');
        }
        $this->Redirect(array('action' => 'index'));
    }

}
